package android.fwirk;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;

import android.fwirk.facebook.FacebookClient;
import android.fwirk.obj.CustomWebClient;
import android.fwirk.obj.CustomWebView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.widget.Button;
import android.widget.ProgressBar;

public class LoginFActivity extends Activity implements OnClickListener {
	
	Button continueB;
	CustomWebView fconnect;
	ProgressBar pBar;
	AlertDialog about;
	Intent intent;
	private final int DIALOG_ABOUT=0;
	private final int DIALOG_NOTLOGGED=1;
	private final int MENU_LOGIN=0;
	private final int MENU_LOGOUT=1;
	private final int MENU_CHECK=2;
	private final int MENU_ABOUT=3;
	private int ACTIVITY_LOGIN=0;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getResources().getText(R.string.app_name)+" ~ Sign in");
        setContentView(R.layout.login1);
        continueB=(Button)findViewById(R.id.continueb);
        continueB.setOnClickListener(this);
        pBar=(ProgressBar)findViewById(R.id.facebookProgress);
        pBar.setVisibility(View.GONE);
        fconnect=(CustomWebView)findViewById(R.id.facebookWeb);
        fconnect.setWebViewClient(new CustomWebClient(pBar));
        fconnect.getSettings().setJavaScriptEnabled(true);
        fconnect.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        fconnect.loadUrl(FacebookClient.URL_LOGOUT);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if(requestCode==ACTIVITY_LOGIN) {
    		if(resultCode==Activity.RESULT_OK) {
    			fconnect.loadUrl(FacebookClient.URL_LOGOUT);
    		}
    	}
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	if(id==DIALOG_ABOUT) {
    		Builder bhelp=new Builder(this);
    		bhelp.setMessage("fwirk\n\nA Facebook, Twitter and Plurk status updater on the Android platform\n\nCredits to:\nMel Leandro Navarro 2006-03375\nReynaldo Manguni\n\n")
			.setPositiveButton("Back", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					
				}
			});
    		return bhelp.create();
    	}
    	else if(id==DIALOG_NOTLOGGED) {
    		Builder bhelp = new Builder(this);
			bhelp
					.setMessage(
							"Facebook setup not finished. Continue?")
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Bundle b=new Bundle();
									b.putBoolean("willFacebook", false);
									intent=new Intent(getApplicationContext(),LoginActivity.class);
									intent.putExtras(b);
									startActivityForResult(intent,ACTIVITY_LOGIN);
								}
							})
							.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							});
			return bhelp.create();
    	}
    	return super.onCreateDialog(id);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0,MENU_LOGIN,0,"Login")
		.setIcon(R.drawable.facebooke);
    	menu.add(0,MENU_LOGOUT,0,"Logout")
		.setIcon(R.drawable.facebooke);
    	menu.add(0,MENU_CHECK,0,"Check Permissions")
		.setIcon(R.drawable.facebooke);
    	menu.add(0,MENU_ABOUT,0,"About")
			.setIcon(android.R.drawable.ic_menu_help);
		return true;
	}
	
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	menu.getItem(MENU_LOGIN).setVisible(false);
		menu.getItem(MENU_LOGOUT).setVisible(false);
		menu.getItem(MENU_CHECK).setVisible(false);
    	if(Const.facebook==null || !Const.facebook.isLoggedIn()) {
    		menu.getItem(MENU_LOGIN).setVisible(true);
    	}
    	else {
    		if(Const.facebook.isLoggedIn()) {
    			menu.getItem(MENU_LOGOUT).setVisible(true);
    			if(!Const.facebook.isPermitted()) {
    				menu.getItem(MENU_CHECK).setVisible(true);
    			}
    		}
    	}
    	return true;
    }
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case MENU_LOGIN:
				pBar.setVisibility(View.VISIBLE);
				fconnect.loadUrl(FacebookClient.URL_AUTH);
				break;
			case MENU_LOGOUT:
				pBar.setVisibility(View.VISIBLE);
				fconnect.loadUrl(FacebookClient.URL_LOGOUT);
				break;
			case MENU_CHECK:
				pBar.setVisibility(View.VISIBLE);
				fconnect.loadUrl(FacebookClient.URL_PERM);
				break;
			case MENU_ABOUT:
				showDialog(DIALOG_ABOUT);
				break;
		}
		return true;
	}

	public void onClick(View arg0) {
		if(arg0==continueB) {
			if(Const.facebookToken==null || Const.facebookToken.equals("")) {
				showDialog(DIALOG_NOTLOGGED);
			}
			else {
				Bundle b=new Bundle();
				b.putBoolean("willFacebook", true);
				intent=new Intent(getApplicationContext(),LoginActivity.class);
				intent.putExtras(b);
				startActivityForResult(intent,ACTIVITY_LOGIN);
			}
		}
		
	}
}